﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace HobSoft.SoccerBet2014.Client
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow :  MahApps.Metro.Controls.MetroWindow
    {
        private AccountWindow accountWindow;
        private ConfigWindow configWindow;
        public MainWindow()
        {
            InitializeComponent();
        }

        private object _syncLock = new object();
        private void btnAccount_Click(object sender, RoutedEventArgs e)
        {
            if (accountWindow == null)
            {
                lock (_syncLock)
                {
                    if (accountWindow == null)
                    {
                        accountWindow = new AccountWindow();
                        accountWindow.Owner = this;
                    }
                }
            }
            accountWindow.Show();
        }

        private void btnSetting_Click(object sender, RoutedEventArgs e)
        {
            if (configWindow == null)
            {
                lock (_syncLock)
                {
                    if (configWindow == null)
                    {
                        configWindow = new ConfigWindow();
                        configWindow.Owner = this;
                    }
                }
            }
            configWindow.Show();
        }

        private void MetroWindow_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var dataContext = e.NewValue as ViewModels.BetShellModel;
            var config = dataContext.ConfigShellModel;
            var queryBetting = config.BettingKeyConfiguration.Where(k => k.HotKeyCommand == Configuration.HotKeyCommand.Focus);
            foreach (var keySetting in queryBetting)
            {
                KeyBinding keybinding;
                if (keySetting.Gesture != null)
                    keybinding = new KeyBinding(dataContext.BmBettingCommand, keySetting.Gesture);
                else
                {
                    keybinding = new KeyBinding();
                    keybinding.Key = keySetting.Key;
                    keybinding.Command = dataContext.SetBettingFocusCommand;
                }
                switch (keySetting.Name)
                {
                    case "Focus1":
                        keybinding.CommandParameter = (byte)10;
                        break;
                    case "Focus2":
                        keybinding.CommandParameter = (byte)11;
                        break;
                    case "Focus3":
                        keybinding.CommandParameter = (byte)12;
                        break;
                    default:
                        break;
                }

                this.InputBindings.Add(keybinding);
            }
        }

    }
}
